package com.ag.lwc.service;

import java.util.LinkedList;
import java.util.List;
import java.util.Set;

import javax.annotation.Resource;

import com.ag.bean.TbUser;
import com.ag.contactTree.TreeNode;
import com.ag.contactTree.service.LocalTreeService;
import com.ag.lwc.dao.LwcUserDeptDao;
import com.ag.zhh.vo.UserDept;
import com.ag.zhh.vo.UserDeptContact;

/**
 * 将药监局的第一层部门和一个部门的联系人查出来的service
 * @author user
 *
 */
public class FindfDepService {

	private LwcUserDeptDao lwcUserDeptDao;
	
	@Resource(name = "LocalTreeService")
	private LocalTreeService treeserivce;

	
	/**
	 * 更加部门查子部门
	 * @param deptTypeId
	 * @return
	 */
	public List<UserDept> findFirstLevel(String udid)
	{
		List<UserDept> result=null;
		result = new LinkedList<UserDept>();
	//	result =lwcUserDeptDao.findFirstLevel(udid);
		
		//首先在内存中把部门查出来
		TreeNode node = treeserivce.getTreeNodeById(udid);
		UserDept parentDept = (UserDept)node.getBindData();
		//返回该部门的字结点
		Set<UserDept> depts = parentDept.getTbUserDepts();
		for(UserDept dept:depts)
		{
			if(dept.getA()==null||dept.getA().equals("0"))
				result.add(dept);
		}
	//	result=parentDept.getChilddepts();
		return result;
	}
	
	/**
	 * 查下一层的部门
	 * @param deptId
	 * @return
	 */
	public List<UserDept> findNextLevel(String udid)
	{
		List<UserDept> result=null;
		result = new LinkedList<UserDept>();
	//	result =lwcUserDeptDao.findFirstLevel(udid);
		
		//首先在内存中把部门查出来
		System.out.println("udid: \t"+udid);
		TreeNode node = treeserivce.getTreeNodeById(udid);
		System.out.println("node: "+node.getNodeId());
		UserDept parentDept = (UserDept)node.getBindData();
		//返回该部门的字结点
		Set<UserDept> depts = parentDept.getTbUserDepts();
		for(UserDept dept:depts)
		{
			if(dept.getA()==null||dept.getA().equals("0"))
				result.add(dept);
		}
	//	result=parentDept.getChilddepts();
		return result;
	}
	
	/**
	 * 将部门的联系人列表查出来，传入部门的id
	 * @param udid
	 * @return
	 */
	public List<UserDeptContact> findDeptsUsers(String udid)
	{
		List<UserDeptContact> result = null;
		TreeNode node = treeserivce.getTreeNodeById(udid);
		UserDept dept = (UserDept)node.getBindData();
		Set<UserDeptContact> users = dept.getTbUsers();
		result = new LinkedList<UserDeptContact>();
		for(UserDeptContact user:users)
		{
			result.add(user);
		}
		return result;
	}

	public LwcUserDeptDao getLwcUserDeptDao() {
		return lwcUserDeptDao;
	}

	public void setLwcUserDeptDao(LwcUserDeptDao lwcUserDeptDao) {
		this.lwcUserDeptDao = lwcUserDeptDao;
	}

	public LocalTreeService getTreeserivce() {
		return treeserivce;
	}

	public void setTreeserivce(LocalTreeService treeserivce) {
		this.treeserivce = treeserivce;
	}
}
